<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>表单页面</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../../../lib/layui/css/layui.css" media="all">
  <link href="../../../lib/wangeditor/style.css" rel="stylesheet">
  <link href="../../../css/layout.css" rel="stylesheet">
  <link rel="stylesheet" href="../../../css/public.css" media="all">
  <style>
      body {
          background-color: #ffffff;
      }
  </style>
</head>
<body>
<form class="layui-form" action="" lay-filter="demo-val-filter">
  <div class="layui-form layuimini-form" id="myForm">
    <div class="layui-form-item">
      <label class="layui-form-label required">名称</label>
      <div class="layui-input-block">
        <input type="text" name="name" lay-verify="required" placeholder="请输入" value="" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label required" >地址</label>
      <div class="layui-input-block">
        <input type="text" name="href" lay-verify="required" placeholder="请输入" value="" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">排序</label>
      <div class="layui-input-block">
        <input type="number" name="qOrder" lay-verify="required" placeholder="请输入" value="" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">是否显示</label>
      <div class="layui-input-block">
        <input type="checkbox" name="showFlag" lay-skin="switch" lay-filter="topFlag" lay-text="是|否">
      </div>
    </div>
    <hr />
    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn" type="button">保存</button>
      </div>
    </div>
  </div>
</form>
<script src="../../../lib/layui/layui.js" charset="utf-8"></script>
<script src="../../../lib/wangeditor/index.js"></script>
<script src="../../../api/common.js" charset="utf-8"></script>

<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        // 获取URL参数
        function getQueryString(name) {
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURIComponent(r[2]);
            }
            return null;
        }

        // 获取页面ID
        var id = getQueryString('id');
        console.log(id);

        // 发起AJAX请求
        $.ajax({
            type: 'GET',
            url: local + 'quick/get/' + id,
            dataType: "json",
            headers: {
                'Authorization': `Bearer ${ localStorage.getItem("token") }`,
            },
            contentType: 'application/json;charset=UTF-8',
            success: function (res) {
                if (res.code === 200 && res.data) {
                    // 数据回显到表单
                    form.val('demo-val-filter', {
                        'name': res.data.name,
                        'qOrder': res.data.qOrder,
                        'href': res.data.href,
                        'showFlag': res.data.showFlag ? true : false,
                    });
                    form.render(); // 渲染表单
                } else {
                    layer.alert('获取数据失败', {icon: 5});
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, {icon: 5});
            }
        });

        // 监听表单提交
        form.on('submit(saveBtn)', function (data) {
            var obj = data.field;
            obj.id = id;
            // showFlag处理
            if (obj.showFlag) {
                obj.showFlag = true;
            } else {
                obj.showFlag = false;
            }

            $.ajax({
                type: 'POST',
                url: local + 'quick/update',
                data: JSON.stringify(obj),
                dataType: "json",
                headers: {
                    'Authorization': `Bearer ${localStorage.getItem("token")}`,
                },
                contentType: 'application/json;charset=UTF-8',
                success: function (res) {
                    if (res.code === 200) {
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                        parent.layui.table.reload('currentTableId');
                        return false;
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, {icon: 5});
                }
            });
        });
    });

</script>
</body>
</html>
